<template>
  <el-card class="global-card">
    <template #header>
      <div class="card-header">
        <span>订单列表</span>
      </div>
    </template>
    <AsideSearch
      :is-loading="loadOpts.refresh"
      @on-search="handleClickSearch"
    />
    <el-table
      :data="tableData"
      style="width: 100%; margin: 20px 0"
      border
      highlight-current-row
    >
      <!-- <el-table-column prop="id" label="#ID" min-width="56"></el-table-column> -->
      <el-table-column label="渠道商" min-width="256">
        <template #default="scope">
          <ProfilePanel
            v-if="scope.row.user"
            :avatar="scope.row.user.avatar"
            :nickname="scope.row.user.nickname"
            :remark="scope.row.user.remark"
            isNav
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="orderNo"
        label="订单号"
        min-width="200"
      ></el-table-column>
      <el-table-column label="订单类型" min-width="96">
        <template #default="scope">
          <PipeTags type="order-type" :state="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="金额" min-width="112">
        <template #default="scope">
          <PipeMoney :type="scope.row.type" :money="scope.row.money" />
        </template>
      </el-table-column>
      <el-table-column label="订单状态" min-width="112">
        <template #default="scope">
          <el-tag v-if="scope.row.isAppealed" type="danger">已售后</el-tag>
          <PipeTags v-else type="order" :state="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="付款方式" min-width="96">
        <template #default="scope">
          <PipeTags type="order-type-pay" :state="scope.row.typePay" />
        </template>
      </el-table-column>
      <el-table-column label="下单应用平台" min-width="96">
        <template #default="scope">
          <PipeTags type="order-type-apply" :state="scope.row.typeApply" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" min-width="192">
        <template #default="scope">
          <PipeDatetime :value="scope.row.createTime" />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="72">
        <template #default="scope">
          <el-button
            size="small"
            type="primary"
            link
            :icon="Document"
            @click="handleClickRowDetail(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="pageOpts.page.value"
      :page-sizes="pageOpts.sizes.value"
      :page-size="pageOpts.size.value"
      :layout="pageOpts.layout.value"
      :total="pageOpts.total.value"
      small
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </el-card>

  <DrawerDetail
    v-model="drawerOpts.visible"
    :title="drawerOpts.title"
    :size="drawerOpts.size"
    :is-edit="drawerOpts.isEdit"
    :row-data="drawerOpts.data"
    @on-refresh="requestData"
  />
</template>

<script setup lang="ts">
import { Document } from '@element-plus/icons-vue';
import { reactive, ref } from 'vue';
import { getOrderList } from '@/apis/order';
import DrawerDetail from './detail.vue';
import { InitDrawerOpts, InitLoadOpts, InitPageOpts } from '@/utils/common';
import AsideSearch from './aside-search.vue';

// 加载状态
const loadOpts = reactive(InitLoadOpts());

// 查询数据（默认执行一次）
let searchData: any;
const handleClickSearch = (data: any) => {
  searchData = data;
  requestData();
};

// 列表数据
let tableData = ref<API.order.EntitySchema[]>();

// 分页数据
const pageOpts = InitPageOpts({
  page: 1,
  size: 10
});

// 抽屉
const drawerOpts = reactive(InitDrawerOpts<API.order.EntitySchema>());

// 切换显示条数
const handleSizeChange = (size: number) => {
  pageOpts.size.value = size;
  pageOpts.page.value = 1;
  requestData();
};

// 切换页数
const handleCurrentChange = (page: number) => {
  pageOpts.page.value = page;
  requestData();
};

// 详情
const handleClickRowDetail = (row: API.order.EntitySchema) => {
  drawerOpts.title = '详情';
  drawerOpts.isEdit = true;
  drawerOpts.data = row;
  drawerOpts.visible = true;
};

// 获取分页数据
const requestData = () => {
  const dataOpt = {
    field: 'more',
    page: pageOpts.page.value,
    limit: pageOpts.size.value,

    ...searchData
  };

  // 开始加载
  loadOpts.refresh = true;

  getOrderList(dataOpt)
    .then((res) => {
      tableData.value = res.data.data || [];
      pageOpts.total.value = res.data.total;

      // 加载结束
      loadOpts.refresh = false;
    })
    .catch((err) => {
      console.log('table list error', err);
      // 加载结束
      loadOpts.refresh = false;
    });
};
</script>

<style lang="scss" scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
